var httpUtil = require('../../utils/httpUtil.js');
var area = require('../../utils/area.js')

var areaInfo = []; //所有省市区县数据
var provinces = [];
var citys = [];
var countys = [];

var index = [0, 0, 0];

var cellId;

var t = 0;
var show = false;
var moveY = 200;

Page({
  data: {
    outline: false,
    hidden: false,
    id:'',
    name: '',
    phone: '',
    address: '',
    isdefault: '否',
    show: show,
    provinces: '',
    citys: '',
    countys: '',
    value: [0, 0, 0]
  },
  formName: function(e) {
    this.setData({
      name: e.detail.value
    })
  },
  formphone: function(e) {
    this.setData({
      phone: e.detail.value
    })
  },
  formaddress: function(e) {
    this.setData({
      address: e.detail.value
    })
  },
  switch1Change(e) {
    if (e.detail.value) {
      this.setData({
        isdefault: '是'
      })
      console.log(this.data.isdefault)
    } else {
      this.setData({
        isdefault: '否'
      })
    }
  },
  save: function() {
    var that = this;
    //正则匹配
    var mobile = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
    // var myreg = /^(([0\+]\d{2,3}-)?(0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/;  //判断是否是座机电话
    var isMobile = mobile.exec(that.data.phone)
    if (that.data.name == '' || that.data.phone == '' || that.data.address == '') {
      wx.showModal({
        title: '提示!!!',
        content: '请完整填写信息',
      })
    } else if (!isMobile) {
      wx.showModal({
        title: '提示!!!',
        content: '你输入的电话不符，请重新检查填写',
      })
    } else {
      that.setData({
        hidden: true
      })
      var req = {
        id:that.data.id,
        userid: wx.getStorageSync("userId"),
        address: that.data.address,
        phone: that.data.phone,
        truename: that.data.name,
        province: that.data.province,
        city: that.data.city,
        region: that.data.county,
        isdefault: that.data.isdefault
      }
      httpUtil.post("/crm/usershopaddressupdate", req)
        .then(function(res) {
          that.setData({
            hidden: false
          })
          wx.showToast({
            title: '' + res.data.message + '',
          })

          setTimeout(function () {
            wx.navigateBack({
              url: '/pages/myAddress/myAddress',
            })
          }, 2000)
        }).catch(function(res) {
          console.log(res);
        });
    }


  },
  onLoad: function(options) {
    var that = this;
    that.setData({
      hidden:true
    })
    //获取省市区县数据
    area.getAreaInfo(function(arr) {
      areaInfo = arr;
      //获取省份数据
      getProvinceData(that);
    });
    var req = {
      id: options.id
    }
    httpUtil.post("/crm/usershopaddressinfo", req)
      .then(function(res) {
        if (res.data.status == 200) {
          that.setData({
            hidden:false,
            name: res.data.data.truename,
            phone: res.data.data.phone,
            address: res.data.data.address,
            province: res.data.data.province,
            city: res.data.data.city,
            region: res.data.data.region,
            isdefault: res.data.data.isdefault,
            id: options.id
          })

        } else {
          wx.showToast({
            title: '' + res.data.message + '',
          })
        }
      }).catch(function(res) {
        console.log(res);
      });
  },
  onShow: function () {
    var that = this;
    //获取当前网络状态
    wx.getNetworkType({
      success: function (res) {
        // 返回网络类型, 有效值：
        // wifi/2g/3g/4g/unknown(Android下不常见的网络类型)/none(无网络)
        if (res.networkType == 'none' || res.networkType == 'unknown') {
          that.setData({
            outline: true
          })
        } else {
          that.setData({
            outline: false,
          })
        }
      }
    })

    //监听网络状态
    wx.onNetworkStatusChange(function (res) {
      if (res.networkType == 'none' || res.networkType == 'unknown') {
        that.setData({
          outline: true
        })
      } else {
        that.setData({
          outline: false,
        })
      }
    })
  },
  // ------------------- 分割线 --------------------
  onReady: function() {
    this.animation = wx.createAnimation({
      transformOrigin: "50% 50%",
      duration: 0,
      timingFunction: "ease",
      delay: 0
    })
    this.animation.translateY(200 + 'vh').step();
    this.setData({
      animation: this.animation.export(),
      show: show
    })
  },
  //滑动事件
  bindChange: function(e) {
    var val = e.detail.value
    //判断滑动的是第几个column
    //若省份column做了滑动则定位到地级市和区县第一位
    if (index[0] != val[0]) {
      val[1] = 0;
      val[2] = 0;
      getCityArr(val[0], this); //获取地级市数据
      getCountyInfo(val[0], val[1], this); //获取区县数据
    } else { //若省份column未做滑动，地级市做了滑动则定位区县第一位
      if (index[1] != val[1]) {
        val[2] = 0;
        getCountyInfo(val[0], val[1], this); //获取区县数据
      }
    }
    index = val;
    //更新数据
    this.setData({
      value: [val[0], val[1], val[2]],
      province: provinces[val[0]].text,
      city: citys[val[1]].text,
      county: countys[val[2]].text
    })

  },
  //移动按钮点击事件
  translate: function(e) {
    if (t == 0) {
      moveY = 0;
      show = false;
      t = 1;
    } else {
      moveY = 200;
      show = true;
      t = 0;
    }
    animationEvents(this, moveY, show);

  },
  //隐藏弹窗浮层
  hiddenFloatView(e) {
    moveY = 200;
    show = true;
    t = 0;
    animationEvents(this, moveY, show);

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },
})
// ---------------- 分割线 ----------------
//动画事件
function animationEvents(that, moveY, show) {
  console.log("moveY:" + moveY + "\nshow:" + show);
  that.animation = wx.createAnimation({
    transformOrigin: "50% 50%",
    duration: 400,
    timingFunction: "ease",
    delay: 0
  })
  that.animation.translateY(moveY + 'vh').step()

  that.setData({
    animation: that.animation.export(),
    show: show
  })
}

//获取省份数据
function getProvinceData(that) {
  var num = 0;
  for (var i = 0; i < areaInfo.length; i++) {
    provinces[num] = areaInfo[i];
    num++;
  }
  that.setData({
    provinces: provinces
  })
  //初始化调一次可更改
  getCityArr(0, that);
  getCountyInfo(0, 0, that);
  that.setData({
    province: "北京市",
    city: "北京市",
    county: "东城区",
  })

}
// 获取地级市数据
function getCityArr(count, that) {
  var num = 0 ;
  for (var i = 0; i < provinces[count].children.length; i++) {
    citys[num] = provinces[count].children[i]
    num++;
  }
  if (citys.length == 0) {
    citys[0] = {
      name: ''
    };
  }
  that.setData({
    city: "",
    citys: citys,
    value: [count, 0, 0]
  })
}
// 获取区县数据
function getCountyInfo(column0, column1, that) {
  var num = 0;
  for (var i = 0; i < citys[column1].children.length; i++) {
    countys[num] = citys[column1].children[i]
    num++;
  }
  if (countys.length == 0) {
    countys[0] = {
      name: ''
    };
  }
  that.setData({
    county: "",
    countys: countys,
    value: [column0, column1, 0]
  })
}